package cn.yzx.pethome.system.menu.service.impl;

import cn.yzx.pethome.system.dept.domain.Department;
import cn.yzx.pethome.system.menu.domain.Menu;
import cn.yzx.pethome.system.menu.mapper.MenuMapper;
import cn.yzx.pethome.system.menu.service.IMenuService;
import cn.yzx.pethome.base.service.impl.BaseServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanzixuan
 * @since 2023-02-01
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<Menu> menuTree() {
        List<Menu> menus = menuMapper.findAll();
        List<Menu> menuTree = new ArrayList<>();
        for(Menu menu : menus){
            if(menu.getParentId() == null){
                menuTree.add(menu);
            }else{
                for(Menu parent : menus){
                    if (menu.getParentId() == parent.getId()){
                        parent.getChildren().add(menu);
                    }
                }
            }
        }
        return menuTree;
    }
}
